// QuadInterpolator.cpp: implementation of the CQuadInterpolator class.
//
//////////////////////////////////////////////////////////////////////

#include "../common.h"
#include "QuadInterpolator.h"



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//
CQuadInterpolator::CQuadInterpolator(CQuadPlayState& rstate) : m_rstate(rstate)
{
    //
    m_target =   -1;
    m_step =     0;
    m_iTMs =     0;
    m_animateTimerl = 0;
}

CQuadInterpolator::~CQuadInterpolator()
{
    m_target =   -1;
    m_step =     0;
    m_iTMs =     0;
    m_animateTimerl = 0;
}


//启动动画
void CQuadInterpolator::start(int slide)
{
    m_target = slide; //粒子索引
    m_step = m_rstate.QuerySlideDir(slide);
    m_animateTimerl = TIMEANI_COVER;
    m_iTMs =
#ifdef UNDER_CE
        timeGetTime();
#else   // UNDER_CE
        clock();
#endif
}

//停止动画
void CQuadInterpolator::stop(int slide)
{
    //替换中心粒子
    m_rstate.m_centerSlide = m_target;
    //
    m_step =     0;
    m_target =   -1;
    m_iTMs =     0;
    m_animateTimerl = 0;
}

//
void CQuadInterpolator::update()
{
    //
    float   fcp = 0;
    float   fTimeNow;

    //是否动画
    if (m_step == 0)
    {
        return;
    }

    //
    fTimeNow =
#ifdef UNDER_CE
        timeGetTime();
#else   // UNDER_CE
        clock();
#endif
    fcp = (fTimeNow - m_iTMs) / m_animateTimerl;

    //中心粒子到位检测
    if (fcp > 1.0)
    {
        std::vector<int>::iterator   it;

        //两边索引列表分别增删一个粒子索引
        if (m_step > 0)
        {
            //左移
            m_rstate.m_leftSlides.insert(m_rstate.m_leftSlides.begin(), m_rstate.m_centerSlide);
            m_rstate.m_rightSlides.push_back(m_rstate.m_leftSlides.at(m_rstate.m_leftSlides.size() - 1));
            m_rstate.m_leftSlides.pop_back();

            for (it = m_rstate.m_rightSlides.begin(); it != m_rstate.m_rightSlides.end(); it++)
            {
                if (*it == m_target)
                {
                    m_rstate.m_rightSlides.erase(it);
                    break;
                }
            }
        }
        else
        {
            //右移
            m_rstate.m_rightSlides.insert(m_rstate.m_rightSlides.begin(), m_rstate.m_centerSlide);
            m_rstate.m_leftSlides.push_back(m_rstate.m_rightSlides.at(m_rstate.m_rightSlides.size() - 1));
            m_rstate.m_rightSlides.pop_back();

            for (it = m_rstate.m_leftSlides.begin(); it != m_rstate.m_leftSlides.end(); it++)
            {
                if (*it == m_target)
                {
                    m_rstate.m_leftSlides.erase(it);
                    break;
                }
            }
        }

        //停到中心位置
        stop(m_target);
        m_rstate.reset();

        return;
    }

    //中心粒子动画
    m_rstate.CenterOffsetPos(-m_step * fcp);

    //粒子群位移动画
    m_rstate.SlidesOffsetPos(CVector(m_step * fcp, 0, 0));

    //边侧粒子动画
    m_rstate.SlidesRotateOffsetPos(m_target, m_step * fcp);


}


